package com.example.zhoutao.puzzle.History;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.zhoutao.puzzle.Data.HistoryDataManager;
import com.example.zhoutao.puzzle.R;
import java.util.ArrayList;

/**
 * Created by ZT on 2019.4.24
 */
public class ChildFragment extends Fragment  {

    private ImageView mPhoto;
    private TextView mStep;
    private TextView mDate;
    private TextView mTime;
    private TextView mMode;

    private String key=null;
    private String diff;
    private ListView listView;
    private MyListViewAdapter adapter;
    private HistoryDataManager historyDataManager;
    private ArrayList<HistoryListView> listData;

    private String name;
    private Cursor cursor;

    public static ChildFragment newInstance(String s,String name){
        ChildFragment childFragment=new ChildFragment();
        Bundle bundle =new Bundle();
        bundle.putString("key",s);
        bundle.putString("name",name);
        childFragment.setArguments(bundle);
        return childFragment;
    }



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        Bundle bundle=getArguments();
        key=bundle!=null?bundle.getString("key"):null;
        name=bundle!=null?bundle.getString("name"):null;
        super.onCreate(savedInstanceState);
        if (historyDataManager == null) {
            historyDataManager=new HistoryDataManager(getActivity());
            historyDataManager.openDataBase();
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment, container, false);
        mPhoto=rootView.findViewById(R.id.iv_headPortrait);
        mStep=rootView.findViewById(R.id.tv_step);
        mDate=rootView.findViewById(R.id.tv_date);
        mTime=rootView.findViewById(R.id.tv_time);
        mMode=rootView.findViewById(R.id.tv_mode);
        listView=rootView.findViewById(R.id.lv_history);
        return rootView;
        /**

        Bundle bundle=new Bundle();
        String userName=bundle.getString("username");
        count=bundle.getInt("listCount");


        list=getdata();
        adapter=new MyListViewAdapter(getActivity(),list);
        listView.setAdapter(adapter);


        cursor=historyDataManager.fetchUserHistory(userName,diff);



/**
        dbHelper=new MyDBOpenHelper(rootView.getContext());

        dbRead=dbHelper.getReadableDatabase();
        String sql="select img,step,date,time from user_history where user_name='1' order by time";
        result=dbRead.rawQuery(sql,null);

        int resultCounts=result.getCount();
        if (resultCounts == 0 || !result.moveToFirst()) {
            Toast.makeText(getActivity(), "数据库中无数据", Toast.LENGTH_SHORT).show();

        }else{
            while (!result.isAfterLast()) {
                step=result.getString(result.getColumnIndex("step"))+":\t";
                date=result.getString(result.getColumnIndex("date"))+"\t";
                time=result.getString(result.getColumnIndex("time"))+"\t";
                img=result.getString(result.getColumnIndex(img));

                history.add(img+step+date+time);
                result.moveToNext();
            }
        }

        listView.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.history_item,history));
        dbRead.close();
 */

/**
 * //无限向下滚动
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                if (firstVisibleItem + visibleItemCount == totalItemCount) {
                    list=getdata();
                    adapter.notifyDataSetChanged();
                }
            }
        });*/



    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listData=new ArrayList<HistoryListView>();
        historyDataManager.openDataBase();
        if (key.equals(Content.S3)) {
            diff="1";

        } else if (key.equals(Content.S4)) {
            diff="2";

        } else if (key.equals(Content.S5)) {
            diff="3";

        } else if (key.equals(Content.S6)) {
            diff="4";

        } else if (key.equals(Content.S7)) {
            diff="5";

        }
        cursor=historyDataManager.fetchUserHistory(name,diff);
        while (cursor.moveToNext()) {
            HistoryListView historyListView=new HistoryListView();
            historyListView.setStep(cursor.getString(1));
            historyListView.setDate(cursor.getString(2));
            historyListView.setTime(cursor.getString(3));
            historyListView.setMode(cursor.getString(5));
            byte[] blob=cursor.getBlob(7);
            Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length);
            historyListView.setBitmap(bmp);
            listData.add(historyListView);
        }
        adapter=new MyListViewAdapter(this.getActivity(),listData);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }




}
